using System.Collections.Generic;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
#if UNITY_EDITOR
using Sirenix.OdinInspector;
using UnityEditor;
#endif
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public static class EffectReuseGlobal
    {
        private static EffectReuseConfig _reuseConfig;
        private static EffectPreloadConfig _preloadConfig;

        private static void Initialize()
        {
            
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                _reuseConfig = AssetDatabase.LoadAssetAtPath<EffectReuseConfig>("Assets/" + PathConstant.GetCharacterEffectReuseConfigPath());
                _preloadConfig = AssetDatabase.LoadAssetAtPath<EffectPreloadConfig>("Assets/" + PathConstant.GetCharacterEffectPreloadConfigPath());
            }
            else
#endif
            {
                var rLoader = ResLoader.Create();
                _reuseConfig = rLoader.LoadABAsset<EffectReuseConfig>(PathConstant.GetCharacterEffectReuseConfigPath());
                _preloadConfig = rLoader.LoadABAsset<EffectPreloadConfig>(PathConstant.GetCharacterEffectPreloadConfigPath());
                rLoader.Put2Pool();
            }
        }

        public static void Clear()
        {
            _reuseConfig = null;
            _preloadConfig = null;
        }

        public static string GetHeroEffectPath(string roleName, string effectName)
        {
            if (_reuseConfig == null)
            {
                Initialize();
            }
            var rRoleName = roleName;
            if (_reuseConfig != null && _reuseConfig.resuse.TryGetValue(rRoleName, out var rDict))
            {
                if (!rDict.TryGetValue(effectName, out rRoleName))
                {
                    rRoleName = roleName;
                }
            }
            return PathConstant.GetHeroEffectPath(rRoleName, effectName); 
        }
        
        public static string GetMonsterEffectPath(string roleName, string effectName)
        {
            if (_reuseConfig == null)
            {
                Initialize();
            }
            var rRoleName = roleName;
            if (_reuseConfig != null && _reuseConfig.resuse.TryGetValue(rRoleName, out var rDict))
            {
                rDict.TryGetValue(effectName, out rRoleName);
            }
            return PathConstant.GetMonsterEffectPath(rRoleName, effectName); 
        }
        
        public static List<string> GetEffects(string roleName)
        {
            if (_preloadConfig == null)
            {
                Initialize();
            }
            if (_preloadConfig != null && _preloadConfig.effects.TryGetValue(roleName, out var rList))
            {
                return rList;
            }
            return null; 
        }
    }
}